<?php

// check user login
session_start();
if (isset($_SESSION["user"]) && !empty($_SESSION["user"]) && $_SESSION["user"] == '1') {
//echo $_SESSION["user"];

//include 'function_library.php';


class Admin extends CI_Controller {

	public function __construct() {
            parent::__construct(); 

            $this->load->helper(array('form','url'));
            $this->load->library('form_validation');
	}//////   end function  ///////////////////
 /////////////////////////////////////////////////////////////////////////
        
        
        public function index() {
            
            echo "Oh Hello!";
            
            $this->load->view('ADMIN_nav_bar');
           
            
        }//////   end function index ///////////////////
 /////////////////////////////////////////////////////////////////////////
        
        public function display_gamelogs() {
             $ci =& get_instance();
             
            $query = $ci->doctrine->em->createQuery("select u from ORM\Dynasties2\Gamelog u");
            $data['logdata'] = $query->getResult();
            
            $this->load->view('ADMIN_nav_bar');
            
            $this->load->view('ADMIN_display_gamelogs_view',$data);
            
            
            
        } /// end public function display_gamelogs()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        
         
         public function display_gamedata() {   
             
             $this->load->view('ADMIN_nav_bar');
             
             
            $turndata = $this->doctrine->em->find('ORM\Dynasties2\Currentturn', '1');
            $current_turn = $turndata->getTurnNumber();      
            
            $query = $this->doctrine->em->createQuery("select u from ORM\Dynasties2\Families u");
            $famresult = $query->getResult();
            
            $query = $this->doctrine->em->createQuery("select u from ORM\Dynasties2\Characters u");
            $charresult = $query->getResult();
            
            foreach ($famresult as $row) {
                //print_r($row);
                echo "<br><b>" . $row-> getFamilyName() . "</b>: ";
                $val = $row-> getId();
                
                $query = $this->doctrine->em->createQuery("SELECT u from ORM\Dynasties2\Characters u WHERE u.whichFamily = $val");
                $charresult_narrow = $query->getResult();
                
                foreach ($charresult_narrow as $row2) {
                    //print_r($row);
                    
                    $years_old = ($current_turn - $row2->getBornDate()) / 8;
                    echo $row2->getCharacterName() . " ($years_old), ";
                }
                
            }
            
            
            
        }/// end public function display_gamedata()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        
         public function display_nobility() {   
             
             $this->load->view('ADMIN_nav_bar');
             
             
            $turndata = $this->doctrine->em->find('ORM\Dynasties2\Currentturn', '1');
            $current_turn = $turndata->getTurnNumber();      
            
            $query = $this->doctrine->em->createQuery("select u from ORM\Dynasties2\Families u");
            $fam_result = $query->getResult();
            
            $query = $this->doctrine->em->createQuery("select u from ORM\Dynasties2\Titles u");
            $titles_result = $query->getResult();
            
            $query = $this->doctrine->em->createQuery("select u from ORM\Dynasties2\Claims u");
            $claims_result = $query->getResult();
            
            $query = $this->doctrine->em->createQuery("select u from ORM\Dynasties2\Characters u");
            $char_result = $query->getResult();
            
            foreach ($fam_result as $row) {
                //print_r($row);
                echo "<br><b>" . $row-> getFamilyName() . "</b>: ";
                $val = $row-> getId();
                
                $query = $this->doctrine->em->createQuery("SELECT u from ORM\Dynasties2\Characters u WHERE u.whichFamily = $val");
                $charresult_narrow = $query->getResult();
                
                foreach ($charresult_narrow as $row2) {
                    //print_r($row);
                    if ($row2->getIsRuler() == '1') {
                        $years_old = ($current_turn - $row2->getBornDate()) / 8;
                        echo $row2->getCharacterName() . " (prestige: " . $row2->getPrestige() . ")  ";

                        $char_id = $row2->getId();
                        $query = $this->doctrine->em->createQuery("SELECT u from ORM\Dynasties2\Titles u WHERE u.characterLink = $char_id");
                        $my_titles = $query->getResult();
                        foreach ($my_titles as $row3) {
                            echo "Count of " . $row3->getRegionLink()->getRegionName() . ", ";
                        }
                    }
                    
                    
                }
                
            }
            
            
            
        }/// end public function display_nobility()
////////////////////////////////////////////////////////////////////////////////////////////////
        
        
        
        
    }    /////////////// END: class Main_interface extends CI_Controller 
    
}  //// END:  if (isset($_SESSION["user"]) && !empty($_SESSION["user"]))
else {
   // deny, log, redirect
    
    //$this->load->helper('url');
    //redirect('/dynasties2/start', 'location');
    
}


